package com.h3c.web.rest;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.h3c.dal.common.TableConstant;
import com.h3c.dal.dao.CasssoRoleDAO;
import com.h3c.dal.entity.CasssoRoleDO;
import com.h3c.dal.entity.query.CasssoRoleQuery;
import com.h3c.web.common.BaseResponse;
import com.h3c.web.common.constant.RoleConstant.RoleEnum;
import com.h3c.web.common.constant.SessionConstant;
import com.h3c.web.common.utils.ListUtil;
import com.h3c.web.common.vo.CasssoRoleVO;
import com.h3c.web.support.console.RoleSupport;

/**
 * @ClassName: RoleRest
 * @Description: 角色 restapi
 * @author: d14287
 * @date: 2017年5月31日 下午3:47:26
 */
@Controller
@RequestMapping("console/role")
public class RoleRest {
	public static final Logger logger = LoggerFactory.getLogger(RoleRest.class);
	@Autowired
	private CasssoRoleDAO casssoRoleDAO;
	@Autowired
	private RoleSupport roleSupport;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/query.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse query(HttpServletRequest request) throws Exception {
		CasssoRoleQuery query = new CasssoRoleQuery();
		query.createCriteria().andStatusEqualTo(
				TableConstant.COMMON_STATUS_NORMAL);
		List<CasssoRoleDO> roleList = casssoRoleDAO.selectByExample(query);
		return BaseResponse.trueOf(ListUtil.copyList(roleList,
				CasssoRoleVO.class));
	}

	/**
	 * 返回当前用户角色
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryRole.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse queryRole(HttpServletRequest request) throws Exception {
		String roleId = (String) request.getSession().getAttribute(
				SessionConstant.ROLE_ID);
		RoleEnum role = roleSupport.getRole(roleId);
		Map<String, Object> result = Maps.newHashMap();
		result.put("roleCode", role.name());
		return BaseResponse.trueOf(result);
	}
}
